/* eslint-disable node/prefer-global/process */
import { resolve } from 'node:path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import UnoCSS from 'unocss/vite'
import Compression from 'vite-plugin-compression'
import { viteMockServe } from 'vite-plugin-mock'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd())
  const isBuild = command === 'build'
  return {
    resolve: {
      alias: {
        '@/': `${resolve('src')}/`,
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8888,
      open: true,
      hmr: true,
      proxy: {
        '/api': {
          target: env.VITE_API_BASE_URL, // 后台服务器地址
          changeOrigin: true, // 是否允许不同源
          secure: false, // 支持https
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ['vue', 'pinia', 'vue-router'],
        dts: 'src/types/auto-imports.d.ts',
      }),
      Components({
        dirs: ['src/components'],
        extensions: ['vue'],
        dts: 'src/types/components.d.ts',
        resolvers: [
          IconsResolver({
            prefix: 'icon', // 修改Icon组件前缀，不设置则默认为i,禁用则设置为false
            enabledCollections: ['mdi'], // 指定collection，element-plus图标集为ep
            alias: {}, // collection别名
            customCollections: ['local'], // 自定义图标前缀
          }),
        ],
      }),
      // createSvgIconsPlugin({
      //   // 指定需要缓存的图标文件夹
      //   iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
      //   // 指定 symbolId 格式
      //   symbolId: 'icon-[dir]-[name]',
      //   svgoOptions: isBuild,
      // }),
      Icons({
        compiler: 'vue3',
        customCollections: {
          local: FileSystemIconLoader('src/assets/icons'),
        },
        transform: (svg, _collection, icon) => {
          let content = svg.replace(/ width="\d*" /, ' ').replace(/ height="\d*"/, ' ')
          if (!icon.endsWith('-color')) {
            content = content.replaceAll(/fill="[a-zA-Z#0-9]*"/g, 'fill="currentColor"').replaceAll(/stroke="[a-zA-Z#0-9]*"/g, 'stroke="currentColor"')
          }
          return content
        },
        scale: 1, // 缩放
        autoInstall: true,
        defaultClass: 'inline-block', // 默认类名
        defaultStyle: '', // 默认样式
      }),
      UnoCSS(),
      Compression({
        disable: !isBuild,
        threshold: 10240,
      }),
      viteMockServe({
        mockPath: 'mock',
        ignore: /^_/,
      }),
    ],
    css: {
      preprocessorOptions: {
        less: {
          additionalData: `@import "@/styles/variables.less";`,
        },
      },
    },
    build: {
      target: 'chrome63',
      chunkSizeWarningLimit: 2000,
      // 静态资源打包到dist下的不同目录
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        },
      },
    },
  }
})
